var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({
  data: {
    url: {
      getShopInfo: app.url.baseUrl + "/shop/info",
      getProduct: app.url.baseUrl + "/shop/publicfood",
      getProductType: app.url.baseUrl + "/kind/list",
      getTypeProduct: app.url.baseUrl + "/shop/typeProduct",
      connectionOrEsc: app.url.baseUrl + "/shop/connectionOrEsc",
      isConnection: app.url.baseUrl + "/shop/isCollection",
      isGood: app.url.baseUrl + "/shop/goodOrEsc",
      appointment: app.url.baseUrl + "/shop/appointment",
      comment: app.url.baseUrl + "/shop/comment",
    },
    shopId: 0,
    shopInfo: {},
    products: [],
    productType: [],
    curnav: 0,
    curfav: false,
    comments: [],//评论
    pageNumber: -1,
    isPage: true,
    yyinfo: { persionNum: 1, username: '', phone: '', month: "06", day: "02", hh: "16", mm: "30", startDate: "2017-06-02", endDate: "2016-06-02", startTime: "16:30", endTime: "16:31" },
    animationData: "",
    showModalStatus: false,
    curclass: 0,
  },
  onLoad: function (options) {
    var shopId = options.id
    //shopId = "8861cfd7b09b4927ab50589bb8d044fa"
    //shopId = "d0"
    this.setData({ shopId: shopId })
    this.getShopInfo(shopId);
    this.isConnection()
    this.getComment()
  },
  onReachBottom() {
    this.getComment()
  },
  //获取用户评价
  getComment: function () {
    var that = this
    if (!this.data.isPage) {
      // wx.showToast({
      //   title: app.tips.noPageData,
      // })
      return
    }
    var data = { shopId: this.data.shopId, pageNumber: this.data.pageNumber + 1 }
    var comments = this.data.comments
    app.ajax(this.data.url.comment, data, function (res) {
      var d = res.data
      var b = true;
      if (d.data == null || d.data.length < 20)
        b = false;
      for (var i = 0; i < d.data.length; i++) {
        var obj = d.data[i]
        comments.push(obj)
      }
      that.setData({ comments: comments, isPage: b, pageNumber: that.data.pageNumber + 1 })
    })
  },
  //获取店铺信息
  getShopInfo: function (obj) {
    var data = { id: obj };
    var that = this;
    app.ajax(this.data.url.getShopInfo, data, function (res) {
      var shopInfo = res.data.data;
      if (shopInfo.shortName!=null)
        WxParse.wxParse('html', 'html', shopInfo.shortName, that, 5);
      that.setData({ shopInfo: shopInfo });
    })
  },
  //是否收藏了该店莆
  isConnection: function () {
    var that = this;
    app.ajax(this.data.url.isConnection, { shopId: this.data.shopId }, function (res) {
      var d = res.data
      if (d.data) {
        that.setData({ curfav: d.data })
      }
    })
  },
  //拨打电话
  callPhone: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.shopInfo.shopPhone,
    })
  },
  //用户和商户之间的地图导航
  distance: function () {
    var lng = this.data.shopInfo.loc.coordinates[1]
    var lat = this.data.shopInfo.loc.coordinates[0]
    var name = this.data.shopInfo.name
    var address = this.data.shopInfo.address
    wx.openLocation({
      latitude: lng,
      longitude: lat,
      name: name,
      address: address,
      scale: 28
    })
  },
  //点赞或取消赞
  good: function (e) {
    var that = this
    var pid = e.currentTarget.dataset.pid
    var pgood = !e.currentTarget.dataset.good
    var products = this.data.products
    app.ajax(this.data.url.isGood, { id: pid, isConnection: pgood }, function (res) {
      var d = res.data
      if (d.code == 2000) {
        for (var i = 0; i < products.length; i++) {
          var temp = products[i]
          if (temp.id == pid) {
            temp.isGood = pgood
            if (pgood){
                temp.fabulousNum += 1
            }else{
              if (temp.fabulousNum >=1)
                temp.fabulousNum -= 1
              else
                temp.fabulousNum =0
            }
            that.setData({ products: products })
            return
          }
        }
      }
    })
  },
  //收藏店铺或取消店铺
  faverite_Tap: function () {
    var b = !this.data.curfav
    var that = this;
    app.ajax(this.data.url.connectionOrEsc, { shopId: this.data.shopId, isConnection: b }, function (res) {
      if (res.data.code == 2000) {
        that.setData({
          curfav: b
        });
      }
    })
  },
  //点击店名和菜谱切换
  nav_item_Tap: function (event) {
    var that = this;
    var idx = event.currentTarget.dataset.modelidx;
    if (idx == 1 && this.data.curnav != 1) {
      //获取所有产品
      var data = { id: this.data.shopId ,name:"1"};
      app.ajax(this.data.url.getProduct, data, function (res) {
        that.setData({ products: res.data.data });
      })
      //获取产品类型
      app.ajax(this.data.url.getProductType, data, function (res) {
        that.setData({ productType: res.data.data });
      })
    }
    this.setData({
      curnav: idx
    });
  },
  search_bg_close_tap: function () {
    wx.navigateBack();
  },
  //加载类型菜单
  loadList: function (event) {
    var that = this;
    var inx = event.currentTarget.dataset.modelidx;
    this.setData({
      curclass: inx
    });
    var url = this.data.url.getTypeProduct;
    var data = { productType: inx, ascriptionShop: this.data.shopId }
    if (inx == '0') {
      url = this.data.url.getProduct;
      data = { id: this.data.shopId };
    }
    app.ajax(url, data, function (res) {
      that.setData({ products: res.data.data });
    })
  },
  com_list_tap: function () {
    wx.navigateTo({
      url: '../comlist/comlist'
    })
  },
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //点击预约
  clickbtn: function () {
    var yyinfo = this.data.yyinfo
    var date = new Date()
    var alterDate = new Date(date.getTime() + (7 * 24 * 3600 * 1000))
    var y = date.getFullYear()
    var m = date.getMonth() + 1
    var d = date.getDate()
    var h = date.getHours()
    var mm = date.getMinutes()
    if (m < 10)
      m = "0" + m
    if (d < 10)
      d = "0" + d
    if (h < 10)
      h = "0" + h
    if (mm < 10)
      mm = "0" + mm
    yyinfo.year = y
    yyinfo.month = m
    yyinfo.day = d
    yyinfo.hh = h
    yyinfo.mm = mm
    yyinfo.startDate = y + '-' + m + '-' + d
    yyinfo.endDate = alterDate.getFullYear() + '-' + (alterDate.getMonth() + 1) + '-' + alterDate.getDate()
    console.log(yyinfo.endDate);
    yyinfo.startTime = "09:00";
    yyinfo.endTime = "21:00";
    this.setData({ yyinfo: yyinfo })
    if (this.data.showModalStatus) {
      this.hideModal();
    } else {
      this.showModal();
    }
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  click_cancel: function () {
    console.log("点击取消");
    this.hideModal();
  },
  //预约确定
  click_ok: function () {
    var that = this;
    var yyinfo = this.data.yyinfo
    var tips = null
    if (yyinfo.username.length <= 0)
      tips = "请填写预约人"
    if (yyinfo.phone.length <= 1)
      tips = "请填写电话"
    if (yyinfo.persionNum.length<=0)
      tips = "请输入用餐人数"
    if (isNaN(yyinfo.phone) || yyinfo.phone.length != 11)
      tips = "请填写正确手机电话"
    if (tips != null) {
      wx.showToast({
        title: tips,
      })
      return
    }
    var yyinfo = this.data.yyinfo
    var time = yyinfo.year + "-" + yyinfo.month + "-" + yyinfo.day + " " + yyinfo.hh + ":" + yyinfo.mm
    var data = { ascriptionShop: this.data.shopId, appointmentTimeStr: time, persionNum: yyinfo.persionNum, contacts: yyinfo.username, phone: yyinfo.phone }
    app.ajax(this.data.url.appointment, data, function (res) {
      wx.navigateTo({
        url: '../order/order',
      })
    })
    this.hideModal();
  },
  bindDateChange: function (e) {
    var yyinfo = this.data.yyinfo
    var date_str = e.detail.value.split("-");
    yyinfo.month = date_str[1]
    yyinfo.day = date_str[2]
    this.setData({ yyinfo: yyinfo });
  },
  bindTimeChange: function (e) {
    var yyinfo = this.data.yyinfo
    var time = e.detail.value.split(":");
    if (time[0] == "00" && time[1] == "00") {
      wx.showToast({
        title: '选择的时间错误,请重选',
      })
      return;
    }
    yyinfo.hh = time[0]
    yyinfo.mm = time[1]
    this.setData({ yyinfo: yyinfo });
  },
  input_content: function (e) {
    var i = e.currentTarget.dataset.inl
    var d = e.detail.value;
    var yyinfo = this.data.yyinfo
    if (i == 0)
      yyinfo.persionNum = d
    else if (i == 1)
      yyinfo.username = d
    else if (i == 2)
      yyinfo.phone = d
    this.setData({ yyinfo: yyinfo })
  },
  //预览菜品
  preImage: function (e) {
    var index = e.currentTarget.dataset.index;
    var d = this.data.products[index];
    wx.previewImage({
      urls: [d.productLogo],
    })
  },
  //预览用户回复图片
  preImg:function(e){
    var src = e.currentTarget.dataset.imgadd;
    wx.previewImage({
      urls: [src],
    })
  }
})